-
Notifications
You must be signed in to change notification settings - Fork 6
/
Keyboard.vb
335 lines (318 loc) · 16 KB
/
Keyboard.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
Namespace My
''' <summary>
''' 模拟键盘操作相关函数
''' </summary>
''' <remarks></remarks>
Partial Public NotInheritable Class Keyboard
Private Declare Sub keybd_event Lib "user32.dll" Alias "keybd_event" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Int32, ByVal dwExtraInfo As UInt32)
Private Declare Function MapVirtualKey Lib "user32.dll" Alias "MapVirtualKeyA" (ByVal wCode As UInt32, ByVal wMapType As UInt32) As UInt32
<Flags()> _
Private Enum KeyEvent As Int32
Down = 0
Up = 2
End Enum
''' <summary>
''' 按下单个键位(保持按下状态,要注意,按下+释放才是一次完整的按键过程)
''' </summary>
''' <param name="Key">键位(Windows.Forms.Keys)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Down(ByVal Key As Keys) As Boolean
Try
keybd_event(Key, MapVirtualKey(Key, 0), KeyEvent.Down, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 释放单个键位(取消按下状态,要注意,按下+释放才是一次完整的按键过程)
''' </summary>
''' <param name="Key">键位(Windows.Forms.Keys)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Up(ByVal Key As Keys) As Boolean
Try
keybd_event(Key, MapVirtualKey(Key, 0), KeyEvent.Up, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 点击单个键位(包括按下+释放过程)
''' </summary>
''' <param name="Key">键位(Windows.Forms.Keys)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Click(ByVal Key As Keys) As Boolean
Try
keybd_event(Key, MapVirtualKey(Key, 0), KeyEvent.Down, 0)
keybd_event(Key, MapVirtualKey(Key, 0), KeyEvent.Up, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 点击多个键位,完成组合键(包括按下+释放过程)
''' </summary>
''' <param name="Keys">键位数组(Windows.Forms.Keys)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Click(ByVal Keys As Keys()) As Boolean
Try
For Each Key As Keys In Keys
keybd_event(Key, MapVirtualKey(Key, 0), KeyEvent.Down, 0)
Next
For Each Key As Keys In Keys
keybd_event(Key, MapVirtualKey(Key, 0), KeyEvent.Up, 0)
Next
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 点击多个键位,完成组合键(包括按下+释放过程)
''' </summary>
''' <param name="Keys1">键位1(Windows.Forms.Keys)</param>
''' <param name="Keys2">键位2(Windows.Forms.Keys)</param>
''' <param name="Keys3">键位3(Windows.Forms.Keys)</param>
''' <param name="Keys4">键位4(Windows.Forms.Keys)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Click(Keys1 As Keys, ByVal Keys2 As Keys, Optional ByVal Keys3 As Keys = Nothing, Optional ByVal Keys4 As Keys = Nothing) As Boolean
Try
Dim Temp As New List(Of Keys)
Temp.Add(Keys1)
Temp.Add(Keys2)
If Keys3 <> Nothing Then
Temp.Add(Keys3)
End If
If Keys4 <> Nothing Then
Temp.Add(Keys4)
End If
For Each Key As Keys In Temp
keybd_event(Key, MapVirtualKey(Key, 0), KeyEvent.Down, 0)
Next
For Each Key As Keys In Temp
keybd_event(Key, MapVirtualKey(Key, 0), KeyEvent.Up, 0)
Next
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Declare Function GetAsyncKeyState Lib "user32.dll" Alias "GetAsyncKeyState" (ByVal vKey As Int32) As Int16
<Flags()> _
Private Enum KeyState As Int16
Down1 = -32767
Down2 = -32768
End Enum
''' <summary>
''' 判断单个键位是否处于按下状态
''' </summary>
''' <param name="Key">键位(Windows.Forms.Keys)</param>
''' <returns>是否处于按下状态</returns>
''' <remarks></remarks>
Public Shared Function CheckDown(ByVal Key As Keys) As Boolean
Dim Temp As Int16 = GetAsyncKeyState(Key)
If Temp = KeyState.Down1 Or Temp = KeyState.Down2 Then
Return True
Else
Return False
End If
End Function
''' <summary>
''' 设置CapsLock的状态
''' </summary>
''' <param name="State">是否打开CapsLock</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function SetCapsLock(ByVal State As Boolean) As Boolean
Try
If Not My.Computer.Keyboard.CapsLock = State Then
keybd_event(Keys.CapsLock, MapVirtualKey(Keys.CapsLock, 0), KeyEvent.Down, 0)
keybd_event(Keys.CapsLock, MapVirtualKey(Keys.CapsLock, 0), KeyEvent.Up, 0)
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 设置ScrollLock的状态
''' </summary>
''' <param name="State">是否打开ScrollLock</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function SetScrollLock(ByVal State As Boolean) As Boolean
Try
If Not My.Computer.Keyboard.ScrollLock = State Then
keybd_event(Keys.Scroll, MapVirtualKey(Keys.Scroll, 0), KeyEvent.Down, 0)
keybd_event(Keys.Scroll, MapVirtualKey(Keys.Scroll, 0), KeyEvent.Up, 0)
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 设置NumLock的状态
''' </summary>
''' <param name="State">是否打开NumLock</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function SetNumLock(ByVal State As Boolean) As Boolean
Try
If Not My.Computer.Keyboard.NumLock = State Then
keybd_event(Keys.NumLock, MapVirtualKey(Keys.NumLock, 0), KeyEvent.Down, 0)
keybd_event(Keys.NumLock, MapVirtualKey(Keys.NumLock, 0), KeyEvent.Up, 0)
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 点击多个键位,输入一段字符串(包括按下+释放过程,限定字符串内容)
''' </summary>
''' <param name="KeyString">键位字符串(只支持输入字母、数字、空格、换行、键盘上有的英文特殊符号,其它字符会被忽略)</param>
''' <param name="MillisecondsInterval">输入每个字符的时间间隔(单位毫秒,默认值为0,无时间间隔)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Input(ByVal KeyString As String, Optional ByVal MillisecondsInterval As Integer = 0) As Boolean
Dim AscString As String = "QWERTYUIOP" & "ASDFGHJKL" & "ZXCVBNM" & "1234567890" & " " & vbCrLf
Dim LowerString As String = "qwertyuiop" & "asdfghjkl" & "zxcvbnm"
Dim OemString As String = ";=,-./`[\]'"
Dim ShiftOemString As String = ":+<_>?~{|}"""
Dim OemKeys As Keys() = New Keys() {Keys.Oem1, Keys.Oemplus, Keys.Oemcomma, Keys.OemMinus, Keys.OemPeriod, Keys.Oem2, Keys.Oem3, Keys.Oem4, Keys.Oem5, Keys.Oem6, Keys.Oem7}
Dim ShiftNumString As String = "!@#$%^&*()"
Dim NumKeys As Keys() = New Keys() {Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9, Keys.D0}
Try
Dim KeyArray As Char() = KeyString.ToCharArray()
For N = 0 To KeyArray.Length - 1
Dim Key As Char = KeyArray(N)
If N > 0 And MillisecondsInterval <> 0 Then
Try
System.Threading.Thread.Sleep(MillisecondsInterval)
Catch ex As Exception
End Try
End If
If AscString.Contains(Key) Then
'大写字母、数字、空格(虚拟键码VK值,与字符ASCII值相同)
If Not My.Computer.Keyboard.CapsLock = True Then
keybd_event(Keys.CapsLock, MapVirtualKey(Keys.CapsLock, 0), KeyEvent.Down, 0)
keybd_event(Keys.CapsLock, MapVirtualKey(Keys.CapsLock, 0), KeyEvent.Up, 0)
End If
keybd_event(Asc(Key), MapVirtualKey(Asc(Key), 0), KeyEvent.Down, 0)
keybd_event(Asc(Key), MapVirtualKey(Asc(Key), 0), KeyEvent.Up, 0)
ElseIf LowerString.Contains(Key) Then
'小写字母
If Not My.Computer.Keyboard.CapsLock = False Then
keybd_event(Keys.CapsLock, MapVirtualKey(Keys.CapsLock, 0), KeyEvent.Down, 0)
keybd_event(Keys.CapsLock, MapVirtualKey(Keys.CapsLock, 0), KeyEvent.Up, 0)
End If
Key = Key.ToString.ToUpper()
keybd_event(Asc(Key), MapVirtualKey(Asc(Key), 0), KeyEvent.Down, 0)
keybd_event(Asc(Key), MapVirtualKey(Asc(Key), 0), KeyEvent.Up, 0)
ElseIf OemString.Contains(Key) Then
'OEM键特殊符号
Dim I As Integer = OemString.IndexOf(Key)
keybd_event(OemKeys(I), MapVirtualKey(OemKeys(I), 0), KeyEvent.Down, 0)
keybd_event(OemKeys(I), MapVirtualKey(OemKeys(I), 0), KeyEvent.Up, 0)
ElseIf ShiftOemString.Contains(Key) Then
'Shift+OEM键特殊符号
Dim I As Integer = ShiftOemString.IndexOf(Key)
keybd_event(Keys.ShiftKey, MapVirtualKey(Keys.ShiftKey, 0), KeyEvent.Down, 0)
keybd_event(OemKeys(I), MapVirtualKey(OemKeys(I), 0), KeyEvent.Down, 0)
keybd_event(OemKeys(I), MapVirtualKey(OemKeys(I), 0), KeyEvent.Up, 0)
keybd_event(Keys.ShiftKey, MapVirtualKey(Keys.ShiftKey, 0), KeyEvent.Up, 0)
ElseIf ShiftNumString.Contains(Key) Then
'Shift+数字键特殊符号
Dim I As Integer = ShiftNumString.IndexOf(Key)
keybd_event(Keys.ShiftKey, MapVirtualKey(Keys.ShiftKey, 0), KeyEvent.Down, 0)
keybd_event(NumKeys(I), MapVirtualKey(NumKeys(I), 0), KeyEvent.Down, 0)
keybd_event(NumKeys(I), MapVirtualKey(NumKeys(I), 0), KeyEvent.Up, 0)
keybd_event(Keys.ShiftKey, MapVirtualKey(Keys.ShiftKey, 0), KeyEvent.Up, 0)
End If
Next
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 连续复制粘贴字符,输入一段字符串(使用Ctrl+V组合键,速度太快时可能出错)
''' </summary>
''' <param name="Source">要输入的字符串</param>
''' <param name="MillisecondsInterval">输入每个字符的时间间隔(单位毫秒,默认值为100,实测,在值较小、系统卡顿时,可能会发生字符混乱,建议设置为20以上)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function PasteDelay(ByVal Source As String, Optional ByVal MillisecondsInterval As Integer = 100) As Boolean
Try
Dim UpperString As String = "QWERTYUIOP" & "ASDFGHJKL" & "ZXCVBNM"
For I = 0 To Source.Length - 1
If I > 0 And MillisecondsInterval <> 0 Then
Try
System.Threading.Thread.Sleep(MillisecondsInterval)
Catch ex As Exception
End Try
End If
If UpperString.Contains(Source(I)) Then
'大写字母
If Not My.Computer.Keyboard.CapsLock = True Then
keybd_event(Keys.CapsLock, MapVirtualKey(Keys.CapsLock, 0), KeyEvent.Down, 0)
keybd_event(Keys.CapsLock, MapVirtualKey(Keys.CapsLock, 0), KeyEvent.Up, 0)
End If
End If
System.Windows.Forms.Clipboard.SetText(Source(I))
keybd_event(Keys.ControlKey, MapVirtualKey(Keys.ControlKey, 0), KeyEvent.Down, 0)
keybd_event(Keys.V, MapVirtualKey(Keys.V, 0), KeyEvent.Down, 0)
keybd_event(Keys.V, MapVirtualKey(Keys.V, 0), KeyEvent.Up, 0)
keybd_event(Keys.ControlKey, MapVirtualKey(Keys.ControlKey, 0), KeyEvent.Up, 0)
Next
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 复制粘贴,输入一段字符串(使用Ctrl+V组合键)
''' </summary>
''' <param name="Source">要输入的字符串</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Paste(ByVal Source As String) As Boolean
Try
System.Windows.Forms.Clipboard.SetText(Source)
keybd_event(Keys.ControlKey, MapVirtualKey(Keys.ControlKey, 0), KeyEvent.Down, 0)
keybd_event(Keys.V, MapVirtualKey(Keys.V, 0), KeyEvent.Down, 0)
keybd_event(Keys.V, MapVirtualKey(Keys.V, 0), KeyEvent.Up, 0)
keybd_event(Keys.ControlKey, MapVirtualKey(Keys.ControlKey, 0), KeyEvent.Up, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 复制粘贴,输入一个图片(使用Ctrl+V组合键)
''' </summary>
''' <param name="Source">要输入的图片</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Paste(ByVal Source As Bitmap) As Boolean
Try
System.Windows.Forms.Clipboard.SetImage(Source)
keybd_event(Keys.ControlKey, MapVirtualKey(Keys.ControlKey, 0), KeyEvent.Down, 0)
keybd_event(Keys.V, MapVirtualKey(Keys.V, 0), KeyEvent.Down, 0)
keybd_event(Keys.V, MapVirtualKey(Keys.V, 0), KeyEvent.Up, 0)
keybd_event(Keys.ControlKey, MapVirtualKey(Keys.ControlKey, 0), KeyEvent.Up, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
End Namespace